package com.py.credit.clean.code;

import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Company: WWW.xypy.com.cn
 * @ClassName SeventeenZerosPlusXCheck
 * @Description: 双公示统一社会信用代码17个0加X校验
 * @Params: Test
 * @Author: YaoS
 * @Create: 2018-12-21 11:13
 **/
public class SeventeenZerosPlusXCheck {
    public boolean run(String data, String extend) {
        //17个0加X的正则校验
        String regex = "^([0]{17})([Xx]{1})$";
        Pattern pat = Pattern.compile(regex);
        Matcher matcher = pat.matcher(data);
        if (StringUtils.isEmpty(data.trim())) {
            return true;
        } else if (matcher.matches()) {
            return true;
        }
        try {
            return checkSgsXydm(data);
        } catch (Exception e) {
            return false;
        }
    }

    private boolean checkSgsXydm(String str) {
//代码字符集-代码字符
        final String[] codeNo = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "J", "K", "L", "M", "N", "P", "Q", "R", "T", "U", "W", "X", "Y"};
        //代码字符集-代码字符数值
        final String[] staVal = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30"};
        //各位置序号上的加权因子
        int[] wi = {1, 3, 9, 27, 19, 26, 16, 17, 20, 29, 25, 13, 8, 24, 10, 30, 28};

        //统一代码由十八位的数字或大写英文字母（不适用I、O、Z、S、V）组成，第18位为校验位。
        //第1位为数字或大写英文字母，登记管理部门代码
        //第2位为数字或大写英文字母，机构类别代码
        //第3到8位共6位全为数字登记管理机关行政区划码
        //第9-17位共9位为数字或大写英文字母组织机构代码
        //第18为为数字或者大写的Y

        String regex = "^([0-9ABCDEFGHJKLMNPQRTUWXY]{2})([0-9]{6})([0-9ABCDEFGHJKLMNPQRTUWXY]{9})([0-9A-Z])$";

        Pattern pat = Pattern.compile(regex);

        Matcher matcher = pat.matcher(str);
        if (!matcher.matches()) {
            System.out.println("表达式非法！");
            return false;
        }

        Map<String, String> map = new HashMap<String, String>();
        for (int i = 0; i < codeNo.length; i++) {
            map.put(codeNo[i], staVal[i]);
        }

        String[] all = new String[str.length()];

        all[0] = str.substring(0, str.length() - 1);
        all[1] = str.substring(str.length() - 1, str.length());

        final char[] values = all[0].toCharArray();

        int parity = 0;

        for (int i = 0; i < values.length; i++) {
            final String val = Character.toString(values[i]);
            parity += wi[i] * Integer.parseInt(map.get(val).toString());
        }

        //return codeNo[31 - parity % 31].equals(all[1]);
        System.out.println(parity);

        return codeNo[30 - (parity - 1) % 31].equals(all[1]);
    }


    public static void main(String[] args) {
        String data = "00000000000000000x";
        SeventeenZerosPlusXCheck check = new SeventeenZerosPlusXCheck();
        System.out.println(check.run(data, ""));
    }
}
